package com.drama.api.company.message.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.company.message.dao.CompanyMessageDao;
import com.drama.api.company.message.domain.po.CompanyMessagePO;
import com.drama.api.company.message.domain.vo.CompanyMessageAllSearchVO;
import com.drama.api.company.message.domain.vo.CompanyMessageReadVO;
import com.drama.api.company.message.domain.vo.CompanyMessageSearchVO;
import com.drama.api.company.message.domain.vo.CompanyMessageVO;
import com.drama.api.company.message.service.ICompanyMessageService;
import com.drama.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
@Transactional
@Slf4j
public class CompanyMessageServiceImpl implements ICompanyMessageService {

    @Autowired
    private CompanyMessageDao messageDao;


    @Override
    public List<CompanyMessageVO> getList(CompanyMessageSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        return messageDao.getList(searchVO);
    }


    @Override
    public List<CompanyMessageVO> getAllList(CompanyMessageAllSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        return messageDao.getAllList(searchVO);
    }

    @Override
    public void read(CompanyMessageReadVO vo) {
        CompanyMessagePO companyMessagePO=messageDao.selectByPrimaryKey(vo.getId());
        if(companyMessagePO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "不存在需要标记的信息！");
        }
        companyMessagePO.setMsgState(1);
        companyMessagePO.preUpdate();
        int i = messageDao.updateByPrimaryKeySelective(companyMessagePO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void all_read() {
        Example example=new Example(CompanyMessagePO.class);
        example.and().andEqualTo("companyId",StpUtil.getLoginId());
        CompanyMessagePO companyMessagePO=new CompanyMessagePO();
        companyMessagePO.setMsgState(1);
        int i = messageDao.updateByExampleSelective(companyMessagePO,example);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }


}